package Task1;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        //1. 实现Student类型的集合
        ArrayList<Student> info = new ArrayList<>();
        String str = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        FileWriter fw = null;
        try {
            //3. 启动时系统读取文件中所有的信息
            br = new BufferedReader(new FileReader("StudentInfo.txt"));
            String line;
            while((line = br.readLine())!= null){
                String[] strArray = line.split(",");
                Student stu = new Student();
                stu.setName(strArray[0]);
                stu.setId(Integer.parseInt(strArray[1]));
                stu.setAge(Integer.parseInt(strArray[2]));
                info.add(stu);
            }
            br.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AgeException e) {
            e.printStackTrace();
        } catch (IdException e) {
            e.printStackTrace();
        }

        try {
            //4. 结束时将所有学生信息写入在TXT文件中
            bw = new BufferedWriter(new FileWriter("StudentInfo.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //1.1 打印字符界面实现: 增加、删除、修改、查找、遍历学生信息功能
        while(true) {
            System.out.println("请输入需要操作的功能:");
            // 选择进行的操作
            Scanner sc = new Scanner(System.in);
            String txt = sc.next();
            if (txt.equals("增加")) {
                System.out.println("请输入需要增加的信息，请依次输入：姓名、学号、年龄");
                try {
                    info.add(new Student(sc.next(), sc.nextInt(), sc.nextInt()));
                } catch (AgeException | IdException e) {
                    e.printStackTrace();
                }
                for (Student s : info) {
                    System.out.println("加入的信息为:" + s.toString());
                }
            }

            if (txt.equals("删除")) {
                for (Student all : info) {
                    System.out.println("现在表内的信息为:" + all.toString());
                }
                System.out.println("请输入需要删除的信息，请依次输入：姓名、学号、年龄");
                Student del = null;
                try {
                    del = new Student(sc.next(), sc.nextInt(),sc.nextInt());
                } catch (AgeException | IdException e) {
                    e.printStackTrace();
                }
                if (info.contains(del)) {
                    System.out.println("此条数据存在，可以删除！");
                    info.remove(del);
                    System.out.println("数据删除成功！");
                } else {
                    System.out.println("数据删除失败！");
                }
            }

            if (txt.equals("查找")) {
                System.out.println("请输入需要查找的信息，请依次输入：姓名、学号、年龄");
                try {
                    if (info.contains(new Student(sc.next(), sc.nextInt(), sc.nextInt()))) {
                        System.out.println("此条数据已经查找到了！");
                    } else {
                        System.out.println("查询数据不存在！");
                    }
                } catch (AgeException | IdException e) {
                    e.printStackTrace();
                }
            }

            if (txt.equals("显示所有信息")) {
                if(info.isEmpty()){
                    System.out.println("表内信息为空！");
                }else{
                    for (Student all : info) {
                        System.out.println("表内的信息为:" + all.toString());
                    }
                }

            }

            if(txt.equals("退出查询")){
                System.out.println("欢迎再次使用");
                //3.退出查询时 将所有学生信息写入到文件中
                for(int i = 0; i < info.size(); ++i){
                    Student stu = info.get(i);
                    StringBuilder sb = new StringBuilder();
                    sb.append(stu.getName()).append(",").append(stu.getId()).append(",").append(stu.getAge());
                    try {
                        bw.write(sb.toString());
                        bw.newLine();
                        bw.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            bw.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                break;
            }
        }

    }
}
